package com.mpyy.service.inventory;

import com.mpyy.dao.inventory.ProductBreakageMapperDaoInter;
import com.mpyy.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version 1.0
 * @Description productBreakageServiceImpl
 * @Author JW
 * @Date 2022-03-09 14:27
 */
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Service(value = "productBreakageServiceImpl")
public class ProductBreakageServiceImpl implements ProductBreakageServiceInter {
    @Autowired
    @Qualifier(value = "productBreakageMapperDao")
    private ProductBreakageMapperDaoInter productBreakageMapperDaoInter;

    /**
     * 获取仓库信息
     * @return
     */
    @Override
    public List<DepotInfo> searchDepotInfo() {
        return productBreakageMapperDaoInter.searchDepotInfo();
    }

    /**
     * 获取经办人信息
     * @return
     */
    @Override
    public List<EmployeeInfo> searchHandlePersonInfo() {
        return productBreakageMapperDaoInter.searchHandlePersonInfo();
    }

    @Override
    public List<ProductBreakage> searchProductBreakageInfo(ProductBreakage productBreakage) {
        return productBreakageMapperDaoInter.searchProductBreakageInfo(productBreakage);
    }

    @Override
    public List<ProductBreakageDesc> searchBreakageDescInfo(ProductBreakageDesc productBreakageDesc) {
        return productBreakageMapperDaoInter.searchBreakageDescInfo(productBreakageDesc);
    }

    @Override
    public List<ProductBreakage> searchProductOverflowInfo(ProductBreakage productBreakage) {
        return productBreakageMapperDaoInter.searchProductOverflowInfo(productBreakage);
    }

    @Override
    public List<ProductBreakageDesc> searchOverflowDescInfo(ProductBreakageDesc productBreakageDesc) {
        return productBreakageMapperDaoInter.searchOverflowDescInfo(productBreakageDesc);
    }

    @Override
    public int insertProductBreakageDesc(List<ProductBreakageDesc> productBreakageDescList) {
        return productBreakageMapperDaoInter.insertProductBreakageDesc(productBreakageDescList);
    }

    @Override
    public int insertProductMoreDesc(List<ProductOverflowDesc> productOverflowDescList) {
        return productBreakageMapperDaoInter.insertProductMoreDesc(productOverflowDescList);
    }

    @Override
    public int insertProductBreakage(ProductBreakage productBreakage) {
        return productBreakageMapperDaoInter.insertProductBreakage(productBreakage);
    }

    @Override
    public int insertProductOverflow(ProductOverflow productOverflow) {
        return productBreakageMapperDaoInter.insertProductOverflow(productOverflow);
    }

    /**
     * 判断新增报损信息是否成功
     * @return 是否新增成功
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    @Override
    public boolean insertProBreakageIsSuccess(ProductBreakage productBreakage, List<ProductBreakageDesc> productBreakageDescList) {
        //判断是否新增成功
        boolean insertIsSuccess = true;
        try {
            for(int i = 0; i < productBreakageDescList.size(); i++){
                //调用根据商品编号、批号、仓库获取库存方法
                Integer invNum = productBreakageMapperDaoInter.searchInvNumByProInfo(productBreakageDescList.get(i));
                Integer proBadDescNum = productBreakageDescList.get(i).getProBadDescNum();

                //判断报损数量是否大于库存数量
                if(invNum < proBadDescNum){
                    insertIsSuccess = false;
                    break;
                }

                //调用更改对应的商品库存数量方法
                int changeRow = productBreakageMapperDaoInter.updateInventoryBadProNum(productBreakageDescList.get(i));

                if(1 != changeRow){
                    insertIsSuccess = false;
                    break;
                }
            }

            if(insertIsSuccess){
                int row1 = productBreakageMapperDaoInter.insertProductBreakage(productBreakage);
                int row2 = productBreakageMapperDaoInter.insertProductBreakageDesc(productBreakageDescList);
                //调用报损新增成功后新增库存变动信息方法
                int saveRow = productBreakageMapperDaoInter.insertInvTurnBreakNum(productBreakageDescList);
                if(row1 != 1 || row2 != productBreakageDescList.size() || saveRow != productBreakageDescList.size()){
                    insertIsSuccess = false;
                }
            }
        }catch (Exception e){
            insertIsSuccess = false;
        }

        return insertIsSuccess;
    }

    /**
     * 判断新增报溢信息是否成功
     * @return 是否新增成功
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    @Override
    public boolean insertProMoreIsSuccess(ProductOverflow productOverflow, List<ProductOverflowDesc> productOverflowDescList) {
        //判断是否新增成功
        boolean insertIsSuccess = true;
        try {
            for(int i = 0; i < productOverflowDescList.size(); i++){
                //调用更改对应的商品库存数量方法
                int changeRow = productBreakageMapperDaoInter.updateInventoryMoreProNum(productOverflowDescList.get(i));
                if(1 != changeRow){
                    insertIsSuccess = false;
                    break;
                }
            }

            if(insertIsSuccess){
                int row1 = productBreakageMapperDaoInter.insertProductOverflow(productOverflow);
                int row2 = productBreakageMapperDaoInter.insertProductMoreDesc(productOverflowDescList);
                //调用报溢新增成功后新增库存变动信息方法
                int saveRow = productBreakageMapperDaoInter.insertInvTurnMoreNum(productOverflowDescList);
                if(row1 != 1 || row2 != productOverflowDescList.size() || saveRow != productOverflowDescList.size()){
                    insertIsSuccess = false;
                }
            }
        }catch (Exception e){
            insertIsSuccess = false;
        }

        return insertIsSuccess;
    }

    /**
     * 报损新增成功后更改对应的商品库存数量
     * @param productBreakageDesc 报损详情对象
     * @return 更改行数
     */
    @Override
    public int updateInventoryBadProNum(ProductBreakageDesc productBreakageDesc) {
        return productBreakageMapperDaoInter.updateInventoryBadProNum(productBreakageDesc);
    }

    /**
     * 报溢新增成功后更改对应的商品库存数量
     * @param productOverflowDesc 报溢详情对象
     * @return 更改行数
     */
    @Override
    public int updateInventoryMoreProNum(ProductOverflowDesc productOverflowDesc) {
        return productBreakageMapperDaoInter.updateInventoryMoreProNum(productOverflowDesc);
    }

    /**
     * 报损新增成功后新增库存变动信息
     * @param productBreakageDescList 报损详情集合
     * @return 新增行数
     */
    @Override
    public int insertInvTurnBreakNum(List<ProductBreakageDesc> productBreakageDescList) {
        return productBreakageMapperDaoInter.insertInvTurnBreakNum(productBreakageDescList);
    }

    /**
     * 报溢新增成功后新增库存变动信息
     * @param productOverflowDescList 报溢详情集合
     * @return 新增行数
     */
    @Override
    public int insertInvTurnMoreNum(List<ProductOverflowDesc> productOverflowDescList) {
        return productBreakageMapperDaoInter.insertInvTurnMoreNum(productOverflowDescList);
    }

    /**
     * 根据商品编号、批号、仓库获取库存
     * @param productBreakageDesc 报损明细对象
     * @return 库存
     */
    @Override
    public Integer searchInvNumByProInfo(ProductBreakageDesc productBreakageDesc) {
        return productBreakageMapperDaoInter.searchInvNumByProInfo(productBreakageDesc);
    }

    /**
     * 查询树形控件
     * @return
     */
    @Override
    public List<ProductType> searchTreeControl() {
        return productBreakageMapperDaoInter.searchTreeControl(null);
    }
}
